package com.example.utils;

import com.alibaba.fastjson.JSON;
import com.example.common.ResponseModel;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

public class ResponseModelUtil {

    public static Mono<Void> result(ServerHttpResponse response, ResponseModel responseModel, HttpStatus httpStatus){
        response.setStatusCode(httpStatus);
        DataBuffer buffer = response.bufferFactory().wrap(JSON.toJSONString(responseModel).getBytes(StandardCharsets.UTF_8));
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    public static Mono<Void> result(ServerHttpResponse response, ResponseModel responseModel){
        response.setStatusCode(HttpStatus.OK);
        DataBuffer buffer = response.bufferFactory().wrap(JSON.toJSONString(responseModel).getBytes(StandardCharsets.UTF_8));
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }
}
